package com.aivin.popuwindowdemo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.aivin.popuwindowdemo.R;
import com.aivin.popuwindowdemo.callback.SelectCallBack;

import java.util.List;

public class PopuwindowItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private int mSelectedItem =  0;
    private List<String> datas;
    private SelectCallBack selectCallBack;
    private Context mContext;


    public PopuwindowItemAdapter(List<String> datas, SelectCallBack selectCallBack) {
        this.datas = datas;
        this.selectCallBack = selectCallBack;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        mContext = viewGroup.getContext();
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_window_set, viewGroup, false);
        return new ChooseHolder(view);
    }



    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof ChooseHolder) {
            ChooseHolder chooseHolder = (ChooseHolder) viewHolder;
            if (position == mSelectedItem) {
                setDrawable(chooseHolder.item_setting_name, R.mipmap.ico_window_check_ok);
            } else {
                chooseHolder.item_setting_name.setCompoundDrawables(null,null,null,null);
            }
            chooseHolder.item_setting_name.setText( datas.get(position));
        }
    }

    private void setDrawable(TextView textView ,int drawableId){
        Drawable drawable= mContext.getResources().getDrawable(drawableId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(null,null,drawable,null);
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }


    @Override
    public int getItemCount() {
        return datas==null? 0: datas.size() ;
    }

    class ChooseHolder extends RecyclerView.ViewHolder {
        TextView item_setting_name;
        ChooseHolder(View itemView) {
            super(itemView);
            item_setting_name = itemView.findViewById(R.id.item_setting_name);
            View.OnClickListener clickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mSelectedItem = getAdapterPosition();
                    selectCallBack.setSelectedCallBack(datas.get(mSelectedItem));
                    notifyDataSetChanged();
                }
            };
            item_setting_name.setOnClickListener(clickListener);
        }
    }

}


